/*
 * Licensed to Nanjing RedOrange Co. ltd (RedOrange) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * RedOrange licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package redora.rdo.api;

import java.util.Set;

import redora.rdo.exceptions.ObjectNotFoundException;

/**
 * PersistableSet is a Set used for parent / child one to many relations. Beside
 * all Set and Collection method, PersistableSet adds some convenience methods
 * specific for the use of Persistable objects. When you try to use classes that
 * do not implement Persistable you will get ClassCastExceptions.<br />
 * The PersistentSet implementation will do some under water housekeeping to add
 * and remove objects in the set into persistence. For example remove object
 * will be marked for deletion in the database (getRemovedObjcts()), adding new
 * object is simplified with:<br />
 * PersistableObject o = insert(new PersistableObject());. This method will
 * create the new object and return an handle to the new object.<br />
 * Also the Persistable objects can be retrieved by their id's with getById()
 * and removeById().
 * 
 * @author jaap
 * @see Persistable
 */
public interface PersistableSet<E> extends Set<E> {
	/**
	 * Removed the object by it's object.id. Only works for an object that has
	 * been persisted (and therefore has an id.
	 */
	public abstract boolean removeById(Long id);

	/** Retrieve the object by it's id (only for persisted objects) */
	public abstract E getById(Long id);

	/**
	 * Convenience method. Inserts a new object to the set and returns this
	 * object. The object is not persisted and does not have an id.
	 */
	public abstract E insert(E e);

	/**
	 * Persistence Layer method. Set's the id for a newly added object. This
	 * method is only used by the persistence layer just after the set is
	 * persisted. Before persisting the map's key value is negative, after this
	 * method the key value is updated to ID.
	 */
	public abstract void setMapKey(E p) throws ObjectNotFoundException;

	/**
	 * Persistence Layer method. Removed objects are recorded so the persistence
	 * layer knows what objects should be removed when the set is persisted.
	 * After persisting, this set is set to empty.
	 */
	public abstract Set<E> getRemovedObjects();

	/**
	 * Persistence Layer method. True when the set is retrieved from the
	 * database (lazy loading) or when the parent object is new.
	 */
	public abstract boolean isRetrieved();

	/** Persistence Layer method. @see #isRetrieved() */
	public abstract void setRetrieved(boolean retrieved);

	/** Indicates if any member is added, removed or changed */
	public abstract boolean isDirty();
}
